package logic

import (
	"context"
	"fmt"
	"ykone/models/mysql"
	"ykone/models/redis"

	"ykone/ykonerpc/internal/svc"
	"ykone/ykonerpc/ykonerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProduceCollectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProduceCollectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProduceCollectLogic {
	return &ProduceCollectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProduceCollectLogic) ProduceCollect(in *ykonerpc.ProduceCollectRequest) (*ykonerpc.ProduceCollectResponse, error) {
	// todo: add your logic here and delete this line

	var product mysql.Produce
	mysql.DB.Debug().Where("product_name = ?", in.ProductName).Find(&product)

	if product.ID == 0 {
		return nil, fmt.Errorf("商品不存在请重新输入")
	}

	redis.Client.ZIncrBy("collectlist", 1, product.ProductName)

	return &ykonerpc.ProduceCollectResponse{}, nil
}
